package com.example.exam.controller;

import com.example.exam.entity.Grade;
import com.example.exam.entity.Student;
import com.example.exam.service.GradeService;
import com.example.exam.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

/**
 * (Grade)表控制层
 *
 * @author makejava
 * @since 2021-09-04 15:02:44
 */
@RestController
@RequestMapping("grade")
public class GradeController {
    /**
     * 服务对象
     */
    @Autowired
    private GradeService gradeService;
    @Autowired
    private StudentService studentService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Grade selectOne(Integer id) {
        return this.gradeService.queryById(id);
    }

    @RequestMapping("/allGrade")
    public List<Grade> AllGrade(HttpSession session) {
        Integer assId = (Integer) session.getAttribute("userId");
        List<Student> studentList = studentService.queryByAssId(assId);
        List<Integer> studentId = new LinkedList<>();
        for (Student student : studentList) {
            studentId.add(student.getStuid());
        }
        List<Grade> res = new LinkedList<>();
        for (Integer integer : studentId) {
            List<Grade> temp = gradeService.queryByStuId(integer);
            for (Grade grade : temp) {
                res.add(grade);
            }
        }
        return res;
    }

    @RequestMapping("/detail/{id}")
    public Student studentDetail(@PathVariable("id") Integer id) {
        return studentService.queryById(id);
    }

}